/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Commentrates;
import entity.Comments;
import entity.Enrollment;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.IllegalOrphanException;
import jpa.controllers.exceptions.NonexistentEntityException;

/**
 *
 * @author atap
 */
public class CommentsJpaController implements Serializable {

    public CommentsJpaController() {
    }
    
    public CommentsJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Comments comments) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Commentrates commentrates = comments.getCommentrates();
            if (commentrates != null) {
                commentrates = em.getReference(commentrates.getClass(), commentrates.getId());
                comments.setCommentrates(commentrates);
            }
            Enrollment enrollmentId = comments.getEnrollmentId();
            if (enrollmentId != null) {
                enrollmentId = em.getReference(enrollmentId.getClass(), enrollmentId.getId());
                comments.setEnrollmentId(enrollmentId);
            }
            em.persist(comments);
            if (commentrates != null) {
                Comments oldCommentsOfCommentrates = commentrates.getComments();
                if (oldCommentsOfCommentrates != null) {
                    oldCommentsOfCommentrates.setCommentrates(null);
                    oldCommentsOfCommentrates = em.merge(oldCommentsOfCommentrates);
                }
                commentrates.setComments(comments);
                commentrates = em.merge(commentrates);
            }
            if (enrollmentId != null) {
                enrollmentId.getCommentsList().add(comments);
                enrollmentId = em.merge(enrollmentId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Comments comments) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comments persistentComments = em.find(Comments.class, comments.getId());
            Commentrates commentratesOld = persistentComments.getCommentrates();
            Commentrates commentratesNew = comments.getCommentrates();
            Enrollment enrollmentIdOld = persistentComments.getEnrollmentId();
            Enrollment enrollmentIdNew = comments.getEnrollmentId();
            List<String> illegalOrphanMessages = null;
            if (commentratesOld != null && !commentratesOld.equals(commentratesNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Commentrates " + commentratesOld + " since its comments field is not nullable.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (commentratesNew != null) {
                commentratesNew = em.getReference(commentratesNew.getClass(), commentratesNew.getId());
                comments.setCommentrates(commentratesNew);
            }
            if (enrollmentIdNew != null) {
                enrollmentIdNew = em.getReference(enrollmentIdNew.getClass(), enrollmentIdNew.getId());
                comments.setEnrollmentId(enrollmentIdNew);
            }
            comments = em.merge(comments);
            if (commentratesNew != null && !commentratesNew.equals(commentratesOld)) {
                Comments oldCommentsOfCommentrates = commentratesNew.getComments();
                if (oldCommentsOfCommentrates != null) {
                    oldCommentsOfCommentrates.setCommentrates(null);
                    oldCommentsOfCommentrates = em.merge(oldCommentsOfCommentrates);
                }
                commentratesNew.setComments(comments);
                commentratesNew = em.merge(commentratesNew);
            }
            if (enrollmentIdOld != null && !enrollmentIdOld.equals(enrollmentIdNew)) {
                enrollmentIdOld.getCommentsList().remove(comments);
                enrollmentIdOld = em.merge(enrollmentIdOld);
            }
            if (enrollmentIdNew != null && !enrollmentIdNew.equals(enrollmentIdOld)) {
                enrollmentIdNew.getCommentsList().add(comments);
                enrollmentIdNew = em.merge(enrollmentIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = comments.getId();
                if (findComments(id) == null) {
                    throw new NonexistentEntityException("The comments with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comments comments;
            try {
                comments = em.getReference(Comments.class, id);
                comments.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The comments with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Commentrates commentratesOrphanCheck = comments.getCommentrates();
            if (commentratesOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Comments (" + comments + ") cannot be destroyed since the Commentrates " + commentratesOrphanCheck + " in its commentrates field has a non-nullable comments field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Enrollment enrollmentId = comments.getEnrollmentId();
            if (enrollmentId != null) {
                enrollmentId.getCommentsList().remove(comments);
                enrollmentId = em.merge(enrollmentId);
            }
            em.remove(comments);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Comments> findCommentsEntities() {
        return findCommentsEntities(true, -1, -1);
    }

    public List<Comments> findCommentsEntities(int maxResults, int firstResult) {
        return findCommentsEntities(false, maxResults, firstResult);
    }

    private List<Comments> findCommentsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Comments.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Comments findComments(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Comments.class, id);
        } finally {
            em.close();
        }
    }

    public int getCommentsCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Comments> rt = cq.from(Comments.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
